//
//  RBGoodsOrderListCell.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/12.
//

#import "RBGoodsOrderListCell.h"
#import "RBGoodsOrderDetailVC.h" //订单详情

@interface RBGoodsOrderListCell()
//背景
@property (nonatomic, strong) UIView *containerView;
//店铺背景
@property (nonatomic, strong) UIView *shopView;
//店铺名称
@property (nonatomic, strong) UILabel *shopNameL;
//状态
@property (nonatomic, strong) UILabel *statusL;
//底部背景
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) RBCellModel *cellModel;

@end

@implementation RBGoodsOrderListCell

//去支付
- (void)clickGoPayAction {
    RBOrderListModel *model = self.cellModel.cellData;
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(1, 0, model);
    }
}
//查看订单
- (void)clickCheckOrderAction {
    RBOrderListModel *model = self.cellModel.cellData;
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(2, 0, model);
    }
}
//取消订单
- (void)clickCancelOrderAction {
    RBOrderListModel *model = self.cellModel.cellData;
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(3, 0, model);
    }
}
//查看物流
- (void)clickCheckLogisticsAction {
    RBOrderListModel *model = self.cellModel.cellData;
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(4, 0, model);
    }
}
//去评价
- (void)clickAppraiseAction {
    RBOrderListModel *model = self.cellModel.cellData;
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(5, 0, model);
    }
}
//确认收货
- (void)clickSureReceiveAction {
    RBOrderListModel *model = self.cellModel.cellData;
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(6, 0, model);
    }
}
//再来一单
- (void)clickAgainOrderAction {
    RBOrderListModel *model = self.cellModel.cellData;
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(7, 0, model);
    }
}
//售后
- (void)clickApplyAfterSalesBtn {
    RBOrderListModel *model = self.cellModel.cellData;
    if (self.cellModel.eventBlock1) {
        self.cellModel.eventBlock1(8, 0, model);
    }
}

- (void)setCellModel:(RBCellModel *)cellModel
{
    _cellModel = cellModel;
    RBOrderListModel *model = cellModel.cellData;
    //清除
    [self.containerView view_clear];
    //店铺
    [self.containerView view_addView:self.shopView withHeight:Zoom(43) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    //店铺信息
    self.shopNameL.text = model.store_name;
    //线
    [self.containerView view_addView:[self lineView] withHeight:1 width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    //商品信息
    [self.containerView view_addView:[self goodsViewWithData:model] withHeight:Zoom(97) width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(0) right:Zoom(0) centerX:0];
    //线
    [self.containerView view_addView:[self lineView] withHeight:1 width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    //底部view
    [self.containerView view_addView:({
        UIView *view = UIView.new;
        self.bottomView = view;
        view;
    }) withHeight:Zoom(30) width:CGFLOAT_MIN top:Zoom(14) bottom:Zoom(13) left:Zoom(12) right:Zoom(12) centerX:0];
    //订单状态
    NSString *statusString = @"";
    NSString *statusColor = @"";
    NSArray *actions;
    //订单状态 0未支付 1已支付待发货 2已发货待确认 3确认收货 4已评价 5退款 6已完成 7已取消
    switch ([model.order_status intValue]) {
        case 0 : {
            statusString = @"待付款";
            statusColor = @"#518CFF";
            actions = @[
                @(RBGoodsOrderActionCheckDetail),
                @(RBGoodsOrderActionCancel),
                @(RBGoodsOrderActionPay)
            ];
        }   break;
        case 1 : {
            statusString = @"待发货";
            statusColor = @"#FFC851";
            actions = @[
                @(RBGoodsOrderActionCheckDetail)
            ];
        }   break;
        case 2 : {
            statusString = @"待收货";
            statusColor = @"#1CB1CC";
            actions = @[
                @(RBGoodsOrderActionCheckDetail),
                @(RBGoodsOrderActionCheckLogistics),
                @(RBGoodsOrderActionConfirmReceive)
            ];
        }   break;
        case 3 : {
            statusString = @"待评价";
            statusColor = @"#FF8651";
            actions = @[
                @(RBGoodsOrderActionCheckDetail),
                @(RBGoodsOrderActionCheckLogistics),
                @(RBGoodsOrderActionAppraise)
            ];
        }   break;
        case 4 : {
            statusString = @"已评价";
            statusColor = @"#0ACA00";
            actions = @[
                @(RBGoodsOrderActionCheckDetail),
                @(RBGoodsOrderActionCheckLogistics),
            ];
        }   break;
        case 5 : {
            statusString = @"退款";
            statusColor = @"#518CFF";
            actions = @[
                @(RBGoodsOrderActionCheckDetail)
            ];
        }   break;
        case 6 : {
            statusString = @"已完成";
            statusColor = @"#0ACA00";
            actions = @[
                @(RBGoodsOrderActionCheckDetail),
                @(RBGoodsOrderActionCheckLogistics),
            ];
        }   break;
        case 7 : {
            statusString = @"已取消";
            statusColor = @"#F92C1A";
            actions = @[
                @(RBGoodsOrderActionCheckDetail),
                @(RBGoodsOrderActionAgain)
            ];
        }   break;
    }
    self.statusL.text = statusString;
    self.statusL.textColor = [UIColor colorWithHexString:statusColor];
    
    [self addActions:actions];
}
//添加按钮
- (void)addActions:(NSArray *)actionsArray {
    [self.bottomView h_view_clear];
    [self.bottomView h_view_addView:UILabel.new withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
    for (NSNumber *number in actionsArray) {
        [self.bottomView h_view_addView:[self buttonWithType:number.integerValue] withHeight:Zoom(30) width:Zoom(75) top:0 bottom:0 left:Zoom(10) right:0 centerY:0];
    }
}

#pragma mark - Getter
//背景
- (UIView *)containerView {
    if (!_containerView) {
        self.containerView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [view rbSetCornerRadius:Zoom(8)];
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
                make.width.mas_equalTo(Zoom(350));
            }];
            view;
        });
    }
    return _containerView;
}
//店铺
- (UIView *)shopView {
    if (!_shopView) {
        self.shopView = ({
            UIView *view = UIView.new;
            
            UIImageView *shopImgView = [RanTool imgViewWithMode:1 radius:0 imgName:@"groupPayShop"];
            [view addSubview:shopImgView];
            [shopImgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(Zoom(19));
                make.height.mas_equalTo(Zoom(17));
                make.left.mas_equalTo(Zoom(16));
                make.centerY.mas_equalTo(0);
            }];
            
            UILabel *shopNameL = [RanTool labelWithFontSize:Zoom(15) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@""];
            self.shopNameL = shopNameL;
            [view addSubview:shopNameL];
            [shopNameL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(0);
                make.width.mas_equalTo(Zoom(160));
                make.left.mas_equalTo(shopImgView.mas_right).offset(Zoom(11));
            }];
            
            UILabel *statusL = [RanTool labelWithFontSize:Zoom(14) weight:2 color:UIColor.blackColor align:0 text:@""];
            self.statusL = statusL;
            [view addSubview:statusL];
            [statusL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-Zoom(12));
                make.centerY.mas_equalTo(0);
            }];

            view;
        });
    }
    return _shopView;
}
//线
- (UIView *)lineView {
    UIView *view = UIView.new;
    view.backgroundColor = [RBThemeConfig lineGrayColor];
    return view;
}
//商品
- (UIView *)goodsViewWithData:(RBOrderListModel *)model {
    UIView *view = UIView.new;
    
    UIImageView *imgView = [RanTool imgViewWithMode:2 radius:Zoom(5) imgName:model.file];
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(15));
        make.top.mas_equalTo(0);
        make.width.height.mas_equalTo(Zoom(97));
    }];
    
    UILabel *nameL = [RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:model.goods_name];
    nameL.numberOfLines = 2;
    [view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(20));
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(15));
    }];
    
    UILabel *countL = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:2 text:[NSString stringWithFormat:@"x%@", model.quantity]];
    [countL setContentHuggingPriority:UILayoutPriorityDefaultHigh + 1 forAxis:UILayoutConstraintAxisHorizontal];
    [countL setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh + 1 forAxis:UILayoutConstraintAxisHorizontal];
    [view addSubview:countL];
    [countL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(nameL);
        make.top.mas_equalTo(nameL.mas_bottom).offset(Zoom(14));
        
    }];
    
    UILabel *priceL = [RanTool labelWithFontSize:Zoom(15) weight:0 color:[RBThemeConfig themeColor] align:0 text:[NSString stringWithFormat:@"¥ %@", model.total_price]];
    [view addSubview:priceL];
    [priceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(nameL);
        make.top.mas_equalTo(countL.mas_bottom).offset(Zoom(12));
    }];
    //    priceL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
    //        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(12)] color:[RBThemeConfig textBlackColor] string:@"实付 "];
    //        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[RBThemeConfig priceRedColor] string:@"¥"];
    //        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(15)] color:[RBThemeConfig priceRedColor] string:@"400.00"];
    //    }];
    
    //申请售后按钮
    if ([model.order_status isEqualToString:@"1"] || [model.order_status isEqualToString:@"2"] ||
        [model.order_status isEqualToString:@"3"] ||
        [model.order_status isEqualToString:@"4"]) {
        NSString *str = @"申请售后";
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn rbSetCornerRadius:Zoom(12.5)];
        [btn setTitle:str forState:UIControlStateNormal];
        btn.adjustsImageWhenHighlighted = NO;
        [btn setTitleColor:[UIColor LC_hex:@"#999999"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        [btn rbSetBorderWitdh:1 borderColor:[UIColor LC_hex:@"#999999"]];
        [btn addTarget:self action:@selector(clickApplyAfterSalesBtn) forControlEvents:UIControlEventTouchUpInside];
        
        [view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(nameL.mas_right).offset(0);
            make.bottom.mas_equalTo(imgView.mas_bottom).offset(5);
            make.width.mas_equalTo(Zoom(78));
            make.height.mas_equalTo(Zoom(25));
        }];
    }
    
    
    return view;
}
//按钮
- (UILabel *)buttonWithType: (RBGoodsOrderAction)type {
    UIColor *color;
    UIColor *bgColor;
    NSString *title;
    UIColor *borderColor;
    SEL sel;
    switch (type) {
        case RBGoodsOrderActionPay : {
            color = UIColor.whiteColor;
            bgColor = [UIColor LC_hex:@"#518CFF"];
            title = @"去支付";
            sel = @selector(clickGoPayAction);
        }   break;
        case RBGoodsOrderActionCheckDetail : {
            color = [RBThemeConfig textGrayColor];
            bgColor = UIColor.whiteColor;
            title = @"查看订单";
            borderColor = [UIColor LC_hex:@"#C1C1C1"];
            sel = @selector(clickCheckOrderAction);
        }   break;
        case RBGoodsOrderActionCancel : {
            color = [UIColor LC_hex:@"#666666"];
            bgColor = [UIColor LC_hex:@"#E2E2E2"];
            title = @"取消订单";
            sel = @selector(clickCancelOrderAction);
        }   break;
        case RBGoodsOrderActionCheckLogistics : {
            color = [UIColor LC_hex:@"#666666"];
            bgColor = [UIColor LC_hex:@"#E2E2E2"];
            title = @"查看物流";
            sel = @selector(clickCheckLogisticsAction);
        }   break;
        case RBGoodsOrderActionConfirmReceive : {
            color = UIColor.whiteColor;
            bgColor = [UIColor LC_hex:@"#518CFF"];
            title = @"确认收货";
            sel = @selector(clickSureReceiveAction);
        }   break;
        case RBGoodsOrderActionAppraise : {
            color = UIColor.whiteColor;
            bgColor = [UIColor LC_hex:@"#518CFF"];
            title = @"评价";
            sel = @selector(clickAppraiseAction);
        }   break;
        case RBGoodsOrderActionAgain : {
            color = UIColor.whiteColor;
            bgColor = [UIColor LC_hex:@"#518CFF"];
            title = @"再来一单";
            sel = @selector(clickAgainOrderAction);
        }   break;
    }
    UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:color align:1 text:title bgColor:bgColor radius:Zoom(15)];
    [view addPriority:1];
    [view rbAddTapGesWithTarget:self action:sel];
    if (borderColor)
    {
        [view rbSetBorderWitdh:1 borderColor:borderColor];
    }
    return view;
}

@end
